//
//  DeviceInfo.m
//  finance
//
//  Created by wac on 14/11/13.
//  Copyright (c) 2014年 com.wacai.licai. All rights reserved.
//

#import "DeviceInfoManager.h"
#import <CommonCrypto/CommonDigest.h>
#import <ifaddrs.h>
#import <arpa/inet.h>
#include <sys/utsname.h>
@implementation DeviceInfoManager

+(NSString*)currentRouterMac {
    
    NSString *macIp = nil;
    CFArrayRef interfacesArray = CNCopySupportedInterfaces();
    if (interfacesArray != nil) {
        CFDictionaryRef networkDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(interfacesArray, 0));
        if (networkDict != nil) {
            NSDictionary *dict = (NSDictionary*)CFBridgingRelease(networkDict);
            
            macIp = [dict valueForKey:@"BSSID"];
        }
        CFRelease(interfacesArray);
    }
    
    NSArray *array = [macIp componentsSeparatedByString:@":"];
    NSMutableString *macAddress = [NSMutableString string];
    
    for (int i = 0; i < [array count]; i++) {
        NSString *space = array[i];
        if ([space length] == 2) {
            [macAddress appendString:space];
        }
        else {//缺0补0
            [macAddress appendString:[NSString stringWithFormat:@"0%@", space]];
        }
    }
    
    return [macAddress uppercaseString];
}

+ (NSString *) macAddress {
    int                    mib[6];
    size_t                len;
    char                *buf;
    unsigned char        *ptr;
    struct if_msghdr    *ifm;
    struct sockaddr_dl    *sdl;
    
    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    
    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error/n");
        return NULL;
    }
    
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1/n");
        return NULL;
    }
    
    if ((buf = malloc(len)) == NULL) {
        printf("Could not allocate memory. error!/n");
        return NULL;
    }
    
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        free(buf);
        printf("Error: sysctl, take 2");
        return NULL;
    }
    
    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    NSString *outstring = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    free(buf);
    return outstring;
    
}

+ (NSString*)generateUUID {
    NSMutableString *  result;
    CFUUIDRef   uuid;
    CFStringRef uuidStr;
    
    uuid = CFUUIDCreate(NULL);
    NSAssert(uuid != NULL, nil);
    
    uuidStr = CFUUIDCreateString(NULL, uuid);
    NSAssert(uuidStr != NULL, nil);
    
    result = [NSMutableString stringWithString:[[NSString stringWithFormat:@"%@", uuidStr] stringByReplacingOccurrencesOfString:@"-" withString:@""]];
    NSAssert(result != nil, nil);
    
    CFRelease(uuidStr);
    CFRelease(uuid);
    
    return result;
}

+ (int)getDeviceType {
    NSString *modelname = [[UIDevice currentDevice]model];
    if ([modelname rangeOfString:@"iPhone"].location != NSNotFound) {
        return 1;
    }
    if ([modelname rangeOfString:@"iPod"].location != NSNotFound) {
        return 2;
    }
    if ([modelname rangeOfString:@"iPad"].location != NSNotFound) {
        return 2;
    }
    return 0;
}

+ (NSString *)getDeviceName {
    NSString *modelname = [[UIDevice currentDevice] model];
    if ([modelname rangeOfString:@"iPhone"].location != NSNotFound) {
        return @"iPhone";
    }
    if ([modelname rangeOfString:@"iPod"].location != NSNotFound) {
        return @"iPod";
    }
    if ([modelname rangeOfString:@"iPad"].location != NSNotFound) {
        return @"iPad";
    }
    return @"UnKnow";
}

+(NSString *)systemInfo {
    return [[UIDevice currentDevice] systemVersion];
}

+(NSString*)idfv {
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=6.0) {
        return [[UIDevice currentDevice].identifierForVendor UUIDString];
    } else {
        return @"";
    }
}

//+(NSString*)idfa {
//    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=6.0) {
//        if ([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
//            return [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
//        }
//    }
//    return @"";
//}

+(BOOL)isJailbroken {
    NSString *cydiaPath = @"/Applications/Cydia.app";
    NSString *aptPath = @"/private/var/lib/apt/";
    return [[NSFileManager defaultManager] fileExistsAtPath:cydiaPath] || [[NSFileManager defaultManager] fileExistsAtPath:aptPath];
}


+ (NSString *)getIPAddress {
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            if(temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    return address;
}

+ (NSString *)currentDeviceName {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *sDeviceModel  = [NSString stringWithCString:systemInfo.machine
                                             encoding:NSUTF8StringEncoding];
    if ([sDeviceModel hasSuffix:@"86"]
        || [sDeviceModel isEqual:@"x86_64"])         return @"Simulator";
    
    // iPhone
    if ([sDeviceModel isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    if ([sDeviceModel isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([sDeviceModel isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    if ([sDeviceModel hasPrefix:@"iPhone3"])         return @"iPhone 4";
    if ([sDeviceModel hasPrefix:@"iPhone4"])         return @"iPhone 4S";
    if ([sDeviceModel isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([sDeviceModel isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    if ([sDeviceModel isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
    if ([sDeviceModel isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
    if ([sDeviceModel hasPrefix:@"iPhone6"])         return @"iPhone 5S";
    if ([sDeviceModel isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    if ([sDeviceModel isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    if ([sDeviceModel isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    if ([sDeviceModel isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    if ([sDeviceModel isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    if ([sDeviceModel isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    if ([sDeviceModel isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    if ([sDeviceModel isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
    if ([sDeviceModel isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
    if ([sDeviceModel isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
    if ([sDeviceModel isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";
    if ([sDeviceModel isEqualToString:@"iPhone10,3"]) return @"iPhone X";
    if ([sDeviceModel isEqualToString:@"iPhone10,6"]) return @"iPhone X";
    if ([sDeviceModel isEqualToString:@"iPhone11,2"]) return @"iPhone XS";
    if ([sDeviceModel isEqualToString:@"iPhone11,4"] ||
        [sDeviceModel isEqualToString:@"iPhone11,6"]) return @"iPhone XS Max";
    if ([sDeviceModel isEqualToString:@"iPhone11,8"]) return @"iPhone XR";
    
    // iPod Touch
    if ([sDeviceModel hasPrefix:@"iPod1"])           return @"iPod Touch 1G";
    if ([sDeviceModel hasPrefix:@"iPod2"])           return @"iPod Touch 2G";
    if ([sDeviceModel hasPrefix:@"iPod3"])           return @"iPod Touch 3G";
    if ([sDeviceModel hasPrefix:@"iPod4"])           return @"iPod Touch 4G";
    if ([sDeviceModel hasPrefix:@"iPod5"])           return @"iPod Touch 5G";
    
    // iPad
    if ([sDeviceModel hasPrefix:@"iPad1"])           return @"iPad 1";
    if ([sDeviceModel isEqualToString:@"iPad2,1"])   return @"iPad 2";
    if ([sDeviceModel isEqualToString:@"iPad2,2"])   return @"iPad 2";
    if ([sDeviceModel isEqualToString:@"iPad2,3"])   return @"iPad 2";
    if ([sDeviceModel isEqualToString:@"iPad2,4"])   return @"iPad 2";
    if ([sDeviceModel isEqualToString:@"iPad2,5"])   return @"iPad Mini 1G";
    if ([sDeviceModel isEqualToString:@"iPad2,6"])   return @"iPad Mini 1G";
    if ([sDeviceModel isEqualToString:@"iPad2,7"])   return @"iPad Mini 1G";
    if ([sDeviceModel isEqualToString:@"iPad3,1"])   return @"iPad 3";
    if ([sDeviceModel isEqualToString:@"iPad3,2"])   return @"iPad 3";
    if ([sDeviceModel isEqualToString:@"iPad3,3"])   return @"iPad 3";
    if ([sDeviceModel isEqualToString:@"iPad3,4"])   return @"iPad 4";
    if ([sDeviceModel isEqualToString:@"iPad3,5"])   return @"iPad 4";
    if ([sDeviceModel isEqualToString:@"iPad3,6"])   return @"iPad 4";
    if ([sDeviceModel isEqualToString:@"iPad4,1"])   return @"iPad Air";
    if ([sDeviceModel isEqualToString:@"iPad4,2"])   return @"iPad Air";
    if ([sDeviceModel isEqualToString:@"iPad4,3"])   return @"iPad Air";
    if ([sDeviceModel isEqualToString:@"iPad4,4"])   return @"iPad Mini 2G";
    if ([sDeviceModel isEqualToString:@"iPad4,5"])   return @"iPad Mini 2G";
    if ([sDeviceModel isEqualToString:@"iPad4,6"])   return @"iPad Mini 2G";
    if ([sDeviceModel isEqualToString:@"iPad4,7"])   return @"iPad Mini 3G";
    if ([sDeviceModel isEqualToString:@"iPad4,8"])   return @"iPad Mini 3G";
    if ([sDeviceModel isEqualToString:@"iPad4,9"])   return @"iPad Mini 3G";
    if ([sDeviceModel isEqualToString:@"iPad5,2"])   return @"iPad Mini 4G";
    if ([sDeviceModel isEqualToString:@"iPad5,3"])   return @"iPad Air 2";
    if ([sDeviceModel isEqualToString:@"iPad5,4"])   return @"iPad Air 2";
    
    // If none was found, send the original string
    return sDeviceModel;
}

+ (NSString *)appName {
    NSDictionary * infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString * app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    return app_Name;
}

@end
